package org.tloss.web.editor.sql.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.EditorPart;
import org.tloss.web.editor.database.editors.jdbc.Column;
import org.tloss.web.editor.database.editors.jdbc.Table;
import org.tloss.web.editor.enity.editors.ColumnAdaptableList;
import org.tloss.web.editor.model.Database;
import org.tloss.web.editor.model.Sql;
import org.tloss.web.editor.utils.Constants;

public class UpdateColumnEditor extends EditorPart implements
		IResourceChangeListener {
	Table table = null;
	Sql sql;
	ColumnAdaptableList adaptableList = new ColumnAdaptableList();

	public Table getDatabase() {
		return table;
	}

	public void setSql(Sql sql) {
		this.sql = sql;
		if (sql != null && Constants.SQL_UPDATE_MODE.equals(sql.getMode())) {
			List<Table> tables = sql.getTables();
			if (tables != null && tables.size() > 0) {
				Table table = tables.get(0);
				Database database = sql.getDatabase();
				if (database != null) {
					List<Table> tables2 = database.getTables();
					int index = -1;
					if (tables2 != null
							&& (index = tables2.indexOf(table)) >= 0) {
						setTable(tables2.get(index));
					}
				}
			}
		}
	}

	CheckboxTreeViewer treeViewer;

	public void doSave(IProgressMonitor monitor) {

	}

	public void setTable(Table table) {
		this.table = table;
		adaptableList.clear();
		List<Column> columns = null;
		if (table != null && (columns = table.getColumns()) != null) {
			for (int i = 0; i < columns.size(); i++) {
				adaptableList.add(new ColumnElement(columns.get(i), this));
			}
			treeViewer.refresh();
		}
	}

	public void doSaveAs() {

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
	}

	public boolean isDirty() {

		return false;
	}

	public boolean isSaveAsAllowed() {

		return false;
	}

	public void createPartControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 2;

		Composite treeContainer = new Composite(composite, SWT.BORDER);
		GridData gridData = new GridData();
		treeContainer.setLayoutData(gridData);
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		TreeColumnLayout treeLayout = new TreeColumnLayout();
		treeContainer.setLayout(treeLayout);
		treeViewer = new CheckboxTreeViewer(treeContainer, SWT.MULTI
				| SWT.FULL_SELECTION);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);

		TreeColumn valueColumn;
		valueColumn = new TreeColumn(tree, SWT.NONE);
		valueColumn.setText("Column");
		treeLayout.setColumnData(valueColumn, new ColumnWeightData(2));

		treeViewer.setContentProvider(new WorkbenchContentProvider());
		// final DatabaseConfigLableProvider treeLabelProvider = new
		// DatabaseConfigLableProvider();
		final WorkbenchLabelProvider labelProvider = new WorkbenchLabelProvider();
		treeViewer.setLabelProvider(new WorkbenchLabelProvider());
		treeViewer.addCheckStateListener(new ICheckStateListener() {

			@Override
			public void checkStateChanged(CheckStateChangedEvent event) {
				if (event.getChecked()) {
					List<Column> updateColumns = sql.getUpdateColumns();
					if (updateColumns == null) {
						updateColumns = new ArrayList<Column>();
						sql.setUpdateColumns(updateColumns);
					}
					if (!updateColumns.contains(event.getElement())) {
						updateColumns.add(((ColumnElement) event.getElement())
								.getColumn());
						resourceChanged(null);
					}
				} else {
					List<Column> updateColumns = sql.getUpdateColumns();
					if (updateColumns == null) {
						updateColumns = new ArrayList<Column>();
						sql.setUpdateColumns(updateColumns);
					}
					if (updateColumns.contains(event.getElement())) {
						updateColumns.remove(event.getElement());
						resourceChanged(null);
					}
				}
			}
		});

		treeViewer.setCheckStateProvider(new ICheckStateProvider() {
			@Override
			public boolean isGrayed(Object element) {

				return false;
			}

			@Override
			public boolean isChecked(Object element) {
				return (sql != null && sql.getUpdateColumns() != null && sql
						.getUpdateColumns().contains(element));
			}
		});

		TreeViewerColumn column2 = new TreeViewerColumn(treeViewer, valueColumn);
		column2.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return labelProvider.getText(element);
			}

			public Image getImage(Object element) {

				return labelProvider.getImage(element);
			}
		});
		Composite wrapper = new Composite(composite, SWT.NONE);
		RowLayout rowLayout = new RowLayout();
		wrapper.setLayout(rowLayout);
		rowLayout.type = SWT.VERTICAL;

		final Button button = new Button(wrapper, SWT.NONE);
		button.setText("Select All");
		button.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent e) {

			}

			@SuppressWarnings("deprecation")
			public void mouseDown(MouseEvent e) {
				treeViewer.setAllChecked(true);
				if (table != null) {
					List<Column> columns = table.getColumns();
					if (columns != null) {
						List<Column> updateColumns = sql.getUpdateColumns();
						if (updateColumns == null) {
							updateColumns = new ArrayList<Column>();
							sql.setUpdateColumns(updateColumns);
						}
						updateColumns.clear();
						updateColumns.addAll(columns);
					}
				}
				resourceChanged(null);
			}

			public void mouseDoubleClick(MouseEvent e) {
			}
		});
		final Button button2 = new Button(wrapper, SWT.NONE);
		button2.setText("Deselect All");

		button2.addMouseListener(new MouseListener() {

			public void mouseUp(MouseEvent e) {

			}

			@SuppressWarnings("deprecation")
			public void mouseDown(MouseEvent e) {
				treeViewer.setAllChecked(false);
				List<Column> updateColumns = sql.getUpdateColumns();
				if (updateColumns == null) {
					updateColumns = new ArrayList<Column>();
					sql.setUpdateColumns(updateColumns);
				}
				updateColumns.clear();
				resourceChanged(null);
			}

			public void mouseDoubleClick(MouseEvent e) {

			}
		});

		getSite().setSelectionProvider(treeViewer);

		treeViewer.setInput(adaptableList);

		treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS);

	}

	public void setFocus() {

	}

	IResourceChangeListener dirtyListener;

	public void setDirtyListener(IResourceChangeListener dirtyListener) {
		this.dirtyListener = dirtyListener;
	}

	@Override
	public void resourceChanged(IResourceChangeEvent arg0) {
		if (dirtyListener != null) {
			dirtyListener.resourceChanged(arg0);
		}
	}

}
